package com.administrators.bag;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wzsck.util.DBManager;
import com.wzsck.util.QueryDataResult;



/**
 * Servlet implementation class DLServlet
 */
@WebServlet("/AdmiDLServlet")
public class DLServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DLServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取参数
				request.setCharacterEncoding("UTF-8");
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/html");
				PrintWriter out=response.getWriter();
				//
				String x=request.getParameter("zhanghao");
				String y=request.getParameter("mima");
			
					String sql = "select * from wzs_administrators where name='"+x+"'";
					System.out.println(sql);
					QueryDataResult qdr = DBManager.executeQuery(sql);
					Administrators a=null;
					if (qdr.rs != null) {
						try {
							while (qdr.rs.next()) {
								String q= qdr.rs.getString("id");
								String z= qdr.rs.getString("name");
								String m = qdr.rs.getString("password");
								a= new Administrators( q,z,m);
								System.out.println(a);
								if(x.equals(z)) {
									if(y.equals(m)) {
										System.out.println(a+"成功");
										request.getRequestDispatcher("SuccessServlet").forward(request, response);
									}else {
										System.out.println(a+"密码错误");
										response.sendRedirect("failServlet");
										return;
									}
								}
							}
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					if(a==null) {
						response.sendRedirect("");
					}
					qdr.close();
					out.flush();
					out.close();
			}
	}


